--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local svrName = 'rt_mpps_service'

local rtmpps_robotMgr = class("rtmpps_robotMgr")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function rtmpps_robotMgr:ctor(message, robotMgr)
	print("---------------------rtmpps_robotMgr ctor")
	self.robotMgr = robotMgr	
	self.msg = message	
	self.tbService = {}
	self.tableRobots = {}
	self.tbCount = {}
	self.gameid = 10
	self:register()
	self:reqAddRoot(2)
end

--注册本服务里的消息
function rtmpps_robotMgr:register()
	self.msg:registerSelf('player_enter_table_res',handler(self,self.player_enter_table_res))
	self.msg:registerSelf('player_leave_table_res',handler(self,self.player_leave_table_res))
	self.msg:registerSelf('nt_settle',handler(self,self.nt_settle))
	--
end

function rtmpps_robotMgr:addRobot(data,user)
	print("____addRobot_____rtmpps_robotMgr__",data, user)
	for k, v in pairs(user) do
		local playerid = v.uid
		if self.tbService[playerid] then 
			self:delRobot({mid = playerid})
		end

		local service = skynet.newservice(svrName)
		local param = {playerid = playerid,  tableid = data.tableid}
		skynet.call(service, 'lua', 'start', param)	
		self.tbService[playerid] = service
		skynet.sleep(100)
	end
end

function rtmpps_robotMgr:delRobot(data)
	local playerid = data.mid
	local service = self.tbService[playerid]
	if service then
		skynet.send(service, 'lua', 'exit')
		self.tbService[playerid] = nil
	end
end

function rtmpps_robotMgr:player_enter_table_res(data)
	self.tableRobots[data.tableid] = self.tableRobots[data.tableid] or {}
	table.insert(self.tableRobots[data.tableid], data.playerid)
end

function rtmpps_robotMgr:player_leave_table_res(data)
	if self.tableRobots[data.tableid] then
		for k, v in ipairs(self.tableRobots[data.tableid] ) do
			if v == data.playerid then
				table.remove(self.tableRobots[data.tableid], k)
				print("------------------------------->player_leave_table_res2")
				self:delRobot({mid = v})
				break
			end			
		end
	end
end

function rtmpps_robotMgr:isRobet(tableid, playerid)
	local tbPlayerid = self.tableRobots[tableid]
	if not table.empty(tbPlayerid) then
		for k, v in pairs(tbPlayerid) do
			if v == playerid then
				return true
			end
		end
	end
	return false
end


--请求添加机器人
function rtmpps_robotMgr:reqAddRoot(num)
	if not num or num == 0 then
		return
	end
	local tbPlayer = skynet.call("manager", "lua", "rb_get_account", num)
	print("------------1-tbPlayer", tbPlayer)
	if table.empty(tbPlayer) then
		return
	end	
	tbPlayer[1].uid = 5000376
	self:addRobot({tableid = 100016}, tbPlayer)
end


return rtmpps_robotMgr